﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;

namespace QLNS
{
    public partial class Giao_dien_tim_kiem : DevExpress.XtraEditors.XtraForm
    {
        // ham de lay danh sach the loai va gan vao combo box TheLoai
        private void LayDSTheLoai()
        {
            List<TheLoaiDTO> dsTheLoai = TheLoaiBUS.LayDSTheLoai();
            cbbTheLoai.DataSource = dsTheLoai;
            cbbTheLoai.DisplayMember = "TenTheLoai";
        }

        public Giao_dien_tim_kiem()
        {
            InitializeComponent();
        }

        private void DuaDSSachVaoLuoi(List<SachDTO> dsSach)
        {

            // dua danh sach sach co duoc vao luoi
            for (int i = 0; i < dsSach.Count; i++)
            {
                gridViewKetQua.Rows.Add(i + 1, dsSach[i].TenSach, dsSach[i].TheLoai.TenTheLoai, dsSach[i].TacGia.TenTacGia, dsSach[i].SoLuongTon);
            } // ket thuc for
        } // ket thuc phuong thuc DuaDSSachVaoLuoi

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            // Xoa cac sach cu con lai trong grid view
            gridViewKetQua.Rows.Clear();

            if(cbbTheLoai.SelectedIndex >= 0)
            {
                List<SachDTO> dsSach;
                TheLoaiDTO theLoai = (TheLoaiDTO)cbbTheLoai.Items[cbbTheLoai.SelectedIndex];
                string maSach = txtMaSach.Text.Trim();
                string tenSach = txtTenSach.Text.Trim();
                string tenTacGia = txtTenTacGia.Text.Trim();

                // tim kiem sach day du voi day du cac field
                if(maSach != "" && tenSach != "" && tenTacGia != "")
                {
                    dsSach = SachBUS.TimKiemSachDayDu(maSach, tenSach, theLoai.TenTheLoai, tenTacGia);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai
                if (maSach == "" && tenSach == "" && tenTacGia == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTenTheLoai(theLoai.TenTheLoai);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai va ma sach
                if (maSach != "" && tenSach == "" && tenTacGia == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiMSach(theLoai.TenTheLoai, maSach);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai va ten sach
                if(tenSach != "" && maSach == "" && tenTacGia == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiTSach(theLoai.TenTheLoai, tenSach);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai va ten tac gia
                if(tenTacGia != "" && maSach == "" && tenSach == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiTTacGia(theLoai.TenTheLoai, tenTacGia);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai, ma sach va ten sach
                if(maSach != "" && tenSach != "" && tenTacGia == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiMSachTSach(theLoai.TenTheLoai, maSach, tenSach);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai, ma sach va ten tac gia
                if(maSach != "" && tenTacGia != "" && tenSach == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiMSachTTacGia(theLoai.TenTheLoai, maSach, tenTacGia);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if

                // tim kiem sach theo ten the loai, ten sach va ten tac gia
                if (tenSach != "" && tenTacGia != "" && maSach == "")
                {
                    dsSach = SachBUS.TimKiemSachTheoTLoaiTSachTTacGia(theLoai.TenTheLoai, tenSach, tenTacGia);
                    DuaDSSachVaoLuoi(dsSach);
                } // ket thuc if       
            } // ket thuc if
        }

        private void Giao_dien_tim_kiem_Load(object sender, EventArgs e)
        {
            LayDSTheLoai();
        }
    }
}